/*
 * BlocoGraphCell.java
 *
 * Created on 3 de Junho de 2007, 00:59
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.jerm.Interface.JGraph_Diagrama;
import java.awt.Color;
import java.util.Collection;
import java.util.HashMap;

import org.jerm.Mapeamento.Atributos.Atributo;
import org.jerm.Mapeamento.Atributos.AtributoBoolean;
import org.jerm.Mapeamento.Atributos.AtributoCaracter;
import org.jerm.Mapeamento.Atributos.AtributoDecimal;
import org.jerm.Mapeamento.Atributos.AtributoEnum;
import org.jerm.Mapeamento.Atributos.AtributoNumero;
import org.jerm.Mapeamento.Atributos.AtributoSerial;
import org.jerm.Mapeamento.Atributos.AtributoTexto;

/**
 *
 * @author Tiago Falcao
 */
public class BlocoAtributosGraphCell extends BlocoGraphCell{
    protected HashMap<String,Atributo> AtributosPrimos;
    protected String viewClass=null;
    protected HashMap<String,Atributo> Atributos;
    
    /** Creates a new instance of BlocoGraphCell */
    public BlocoAtributosGraphCell(String Nome) {
        this(20,20,Nome);
    }
    
    public BlocoAtributosGraphCell(double x,double y, String Nome) {
        this(x,y,80,40,Color.white,Nome);
    }
    
    public BlocoAtributosGraphCell(double x,double y, double w, double h, 
                    Color bg, String Nome){
        this(x,y,w,h,bg,Nome,"");
    }
    
    public BlocoAtributosGraphCell(double x,double y, double w, double h, 
                    Color bg, String Nome, String viewClass){
        super(x,y,w,h,bg,Nome,viewClass);
        
//        GraphConstants.setResize(this.getAttributes(), true);
//        GraphConstants.setAutoSize(this.getAttributes(), true);
//        GraphConstants.setSize(this.getAttributes(), new Dimension((int)w,(int)h));
        
        Atributos=new HashMap<String,Atributo>();
        AtributosPrimos=new HashMap<String,Atributo>();
    }
    
    public boolean addAtributo(Atributo Atributo){
        if(this.Atributos.containsKey(Atributo.getNome()))
            return false;
        Atributos.put(Atributo.getNome(),Atributo);
        return Atributos.containsKey(Atributo.getNome());
    }
    
    public boolean renameAtributo(String NomeAntigo,String NovoNome){
        if(this.Atributos.containsKey(NovoNome) || !this.Atributos.containsKey(NomeAntigo) ){
            if(this.AtributosPrimos.containsKey(NovoNome) || !this.AtributosPrimos.containsKey(NomeAntigo) )
                return false;
            Atributo Atributo=(Atributo)AtributosPrimos.remove(NomeAntigo);
            Atributo.setNome(NovoNome);
            AtributosPrimos.put(Atributo.getNome(),Atributo);
            return true;
        }
        Atributo Atributo=(Atributo)Atributos.remove(NomeAntigo);
        Atributo.setNome(NovoNome);
        Atributos.put(Atributo.getNome(),Atributo);
        return true;
    }
    
    public Atributo removeAtributo(String Nome){
        if(! this.Atributos.containsKey(Nome)){
            if((! this.AtributosPrimos.containsKey(Nome)))
                return null;
            return (Atributo)AtributosPrimos.remove(Nome);
        }
        return (Atributo)Atributos.remove(Nome);
    }
    
    public boolean changeAtributo(String Nome,String NovoTipo){
        Atributo a;
        Atributo b;
        boolean key;
        
        if(!this.Atributos.containsKey(Nome) ){
            if(!this.AtributosPrimos.containsKey(Nome) )
                return false;
            a=(Atributo)AtributosPrimos.remove(Nome);
            key=true;
        }else{
            a=(Atributo)Atributos.remove(Nome);
            key=false;
        }
        
        if(NovoTipo=="serial"||NovoTipo=="bigserial")
            b=new AtributoSerial();
        else if(NovoTipo=="smallint"||NovoTipo=="int"||NovoTipo=="bigint"||NovoTipo=="float"||NovoTipo=="double")
            b=new AtributoNumero();
        else if(NovoTipo=="decimal"||NovoTipo=="numeric")
            b=new AtributoDecimal();
        else if(NovoTipo=="text")
            b=new AtributoTexto();
        else if(NovoTipo=="char"||NovoTipo=="varchar")
            b=new AtributoCaracter();
        else if(NovoTipo=="set"||NovoTipo=="enum")
            b=new AtributoEnum();
        else
            b=new AtributoBoolean();
        
        Object[] o=a.getArray();
        o[1]=NovoTipo;
        b.loadArray(o);
        
        if(key)
            AtributosPrimos.put(b.getNome(),b);
        else
            Atributos.put(b.getNome(),b);
        
        
        return false;
    }
    
    public boolean setPrimo(String Nome,boolean Primo){
        if(! this.Atributos.containsKey(Nome)){
            if(! this.AtributosPrimos.containsKey(Nome))
                return false;
            else if(Primo)
                return true;
            else
                Atributos.put(Nome,AtributosPrimos.remove(Nome));
                return Atributos.containsKey(Nome);
        }else if(! Primo)
            return true;
        else{
            AtributosPrimos.put(Nome,(Atributo)Atributos.remove(Nome));
            return AtributosPrimos.containsKey(Nome);
        }
    }
    
    public Collection<Atributo> valuesAtributos(){
        Collection<Atributo> valores = Atributos.values();
        return valores;
    }
    
    public Collection<Atributo> valuesAtributosPrimos(){
        Collection<Atributo> valores = AtributosPrimos.values();
        return valores;
    }
    
    public Object[] valuesArrayAtributos(){
        Collection<Atributo> valores = Atributos.values();
        return valores.toArray();
    }
    
    public Object[] valuesArrayAtributosPrimos(){
        Collection<Atributo> valores = AtributosPrimos.values();
        return valores.toArray();
    }
    
    public int sizeAtributos(){
        return Atributos.size();
    }
    
    public boolean isEmptyAtributos(){
        return Atributos.isEmpty();
    }
    
    public int sizeAtributosPrimos(){
        return AtributosPrimos.size();
    }
    
    public boolean isEmptyAtributosPrimos(){
        return AtributosPrimos.isEmpty();
    }
    
    public boolean isEmpty(){
        return Atributos.isEmpty() && AtributosPrimos.isEmpty();
    }
}
